package com.xinyiweidai.xinyiweidai.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.Constants;
import com.xinyiweidai.xinyiweidai.MainActivity;
import com.xinyiweidai.xinyiweidai.bean.LoginBean;
import com.xinyiweidai.xinyiweidai.bean.TokenBean;
import com.xinyiweidai.xinyiweidai.util.Api;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().hide();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        new Handler().postDelayed(this::initLogin, 2000);
    }


    private void initLogin() {
        Constants.isLogin = SpUtil.getInstance().getLoginState();
        L.e("   " + Constants.isLogin);
        if (!Constants.isLogin) {
            goMainActivity();
        } else {
            if (Constants.isLogin) {
                tokenCheck();
            }

        }
    }


    private void tokenCheck() {
        //showLoading("正在认证");
        OkHttpUtils.post()
                .url(Api.tokenCheck)
                .addParams("token", SpUtil.getInstance().getUserInfo().getToken())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        if (e.getMessage().contains("401")) {
                            SpUtil.getInstance().clearUserInfo();
                            goMainActivity();
                        } else {
                            MyToast.showError(SplashActivity.this, "网络异常");
                            finish();
                        }
                    }

                    @Override
                    public void onResponse(String response) {
                        L.e("token" + response);
                        TokenBean tokenBean = new Gson().fromJson(response, TokenBean.class);
                        refreshLogin(tokenBean.getData().getToken());
                    }
                });
    }

    //刷新状态
    private void refreshLogin(String token) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        OkHttpUtils.get().url(Api.refresh).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                goMainActivity();
            }

            @Override
            public void onResponse(String response) {
                L.e(response);
                LoginBean bean = new Gson().fromJson(response, LoginBean.class);
                if (bean.getCode() == 1) {
                    SpUtil.getInstance().saveUserInfo(bean.getData().getUserinfo());
                }
                goMainActivity();
            }
        });
    }


    private void goMainActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}
